package com.example.ywj.http.OKHttps;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.ywj.http.R;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpPostActivity extends AppCompatActivity {
    private static final String TAG = "OkHttpPostActivity";
    @BindView(R.id.btn_okhttp_post2)
    public Button btn1;
    @BindView(R.id.okhttp_post_username)
    public EditText et_username;
    @BindView(R.id.okhttp_post_password)
    public EditText et_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ok_http_post);
        ButterKnife.bind(this);
    }
    @OnClick({R.id.btn_okhttp_post2})
    public void onClickView(View v){
        switch (v.getId()){
            case R.id.btn_okhttp_post2:
                getAsyncData();
                break;
        }
    }
    public void getAsyncData(){
        String path = "http://10.15.180.121:3009/getUser";
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
              // .add("username",et_username.getText().toString().trim())
           //     .add("password",et_password.getText().toString().trim())
              .build();
        Request request = new Request.Builder()
                .url(path)
                .post(requestBody)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result =   response.body().string();
                Log.i(TAG, "onResponse: "+result);
            }
        });
    }
}
